package jzqb.dao.impl;

import cnki.tpi.base.DataContext;
import cnki.tpi.base.DataSourceManager;
import cnki.tpi.kbatis.sqlsession.SqlSession;
import cnki.tpi.kbatis.utils.DataSourceUtil;
import haha.life.common.BelongToSqlUtils;
import jzqb.bo.AdminQueryBo;
import jzqb.bo.MonitorBo;
import jzqb.dao.AdminLogDao;
import jzqb.po.AdminLoginLogPo;
import jzqb.po.AdminViewDownLogPo;
import jzqb.po.MonitorPo;
import jzqb.po.VisitPo;
import jzqb.vo.StandardStatisticsVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 日志统计数据层实现类
 */
@Repository
public class AdminLogDaoImpl implements AdminLogDao {

    @Autowired
    private DataSourceManager kbaseDs;

    @Autowired
    private DataContext dataContext;
    private static final Logger logger = LoggerFactory.getLogger(DataContext.class);

    @Override
    public List<StandardStatisticsVo> StandardStatisticsByOne(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.StandardStatisticsByOne";
        return sqlSession.selectList(statementId, bo, false);
    }

    @Override
    public List<StandardStatisticsVo> StandardStatisticsByCondition(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.StandardStatisticsByCondition";
        return sqlSession.selectList(statementId, bo, false);
    }

    @Override
    public Long countAll(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.countAll";
        return sqlSession.getCount(statementId, bo);
    }

    @Override
    public List<StandardStatisticsVo> getBrowseByCondition(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.getBrowseByCondition";
        return sqlSession.selectList(statementId, bo, false);
    }


    @Override
    public List<StandardStatisticsVo> export(AdminQueryBo bo) {
        List<StandardStatisticsVo> result = null;
        String sql = "SELECT 题名 as title,来源数据库 as dbsource, 作者 as author,发表时间 as publishdate,TABLENAME as tablename,下载频次 as download,SYS_FLD_SYSID as sysid,文件名 as filename FROM  " + bo.getDbCode()+ " WHERE ((题名='*' not 状态='*') OR 状态='2')";
        if(!"".equals(bo.getKeyWord()) && bo.getKeyWord() != null && !"".equals(bo.getSearchWay()) && bo.getSearchWay() != null){
            if("全文".equals(bo.getSearchWay())){
                sql += " AND " + bo.getSearchWay() + " %= '"+bo.getKeyWord()+"'";
            }else{
                sql += " AND " + bo.getSearchWay() + " % '"+bo.getKeyWord()+"'";
            }
        }
        if(!"".equals(bo.getBeginDate()) && bo.getBeginDate() != null){
            sql += " AND 发表时间 >=" +"'"+bo.getBeginDate()+"'";
        }
        if(!"".equals(bo.getEndDate()) && bo.getEndDate() != null){
            sql += " AND 发表时间 < '"+bo.getEndDate()+" ' OR 发表时间 = '" +bo.getEndDate()+"'";
        }
        if(!"".equals(bo.getOrderBy()) && bo.getOrderBy() != null){
            sql += " ORDER BY " + bo.getOrderBy();
        }
        long startTime = System.currentTimeMillis();
        System.out.println(sql);
        result = dataContext.excuteQuery(sql,StandardStatisticsVo.class);
        long endTime = System.currentTimeMillis();
        System.out.println("总耗时：" + (endTime - startTime) +" ms");
        return result;
    }

    @Override
    public List<Integer> countPart(AdminQueryBo bo) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.countPart";
        return sqlSession.selectList(statementId, bo, false);
    }


    @Override
    public boolean insertLogRecord(AdminViewDownLogPo po) {
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.insertLogRecord";
        return sqlSession.executeInsert(statementId,po);
    }

    /**
     * 浏览下载统计结果页
     * @param adminViewDownLogPo 对象
     * @return 分页后的结果
     */
    @Override
    public List<AdminViewDownLogPo> cutList(AdminViewDownLogPo adminViewDownLogPo) {
        ArrayList conditionList = new ArrayList();
        String sql = "";
        // 1 不分组
        if ("".equals(adminViewDownLogPo.getGroupType())){
            if (!"".equals(adminViewDownLogPo.getGroupValue())){
                conditionList.add(String.format("用户ID=\"*%s\"" , adminViewDownLogPo.getGroupValue()));
            }
            if (!"".equals(adminViewDownLogPo.getAccessType())){
                conditionList.add("访问类型=" + adminViewDownLogPo.getAccessType());
            }
            if (!"".equals(adminViewDownLogPo.getSource())){
                conditionList.add("资源来源=" + adminViewDownLogPo.getSource());
            }
            if (!"".equals(adminViewDownLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "\"" + adminViewDownLogPo.getStartTime()+ "\"");
            }
            if (!"".equals(adminViewDownLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "\"" + adminViewDownLogPo.getEndTime() + "\"");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select * from YQTLLXZB %s order by %s %s limit %s,%s",whereStr,adminViewDownLogPo.getOrderType(),adminViewDownLogPo.getOrderValue(),adminViewDownLogPo.getStartPage(),adminViewDownLogPo.getPageSize());
        }

        // 2 按 用户id分组
        else if ("worknum".equals(adminViewDownLogPo.getGroupType())){
            if (!"".equals(adminViewDownLogPo.getGroupValue())){
                conditionList.add(String.format("用户ID=\"*%s\"" , adminViewDownLogPo.getGroupValue()));
            }
            if (!"".equals(adminViewDownLogPo.getAccessType())){
                conditionList.add("访问类型=" + adminViewDownLogPo.getAccessType());
            }
            if (!"".equals(adminViewDownLogPo.getSource())){
                conditionList.add("资源来源=" + adminViewDownLogPo.getSource());
            }
            if (!"".equals(adminViewDownLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "\"" + adminViewDownLogPo.getStartTime()+ "\"");
            }
            if (!"".equals(adminViewDownLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "\"" + adminViewDownLogPo.getEndTime() + "\"");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }

            sql = String.format("select 用户ID,count(*) as count from YQTLLXZB %s group by 用户ID order by %s %s limit %s,%s",whereStr,adminViewDownLogPo.getOrderType(),adminViewDownLogPo.getOrderValue(),adminViewDownLogPo.getStartPage(),adminViewDownLogPo.getPageSize());


        }else{
            // 3 按资源名分组
            if (!"".equals(adminViewDownLogPo.getGroupValue())){
                conditionList.add(String.format("用户ID=\"*%s\"" , adminViewDownLogPo.getGroupValue()));
            }
            if (!"".equals(adminViewDownLogPo.getAccessType())){
                conditionList.add("访问类型=" + adminViewDownLogPo.getAccessType());
            }
            if (!"".equals(adminViewDownLogPo.getSource())){
                conditionList.add("资源来源=" + adminViewDownLogPo.getSource());
            }
            if (!"".equals(adminViewDownLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "\"" + adminViewDownLogPo.getStartTime()+ "\"");
            }
            if (!"".equals(adminViewDownLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "\"" + adminViewDownLogPo.getEndTime() + "\"");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select 资源名称,count(*) as count from YQTLLXZB %s group by 资源名称 order by %s %s limit %s,%s",whereStr,adminViewDownLogPo.getOrderType(),adminViewDownLogPo.getOrderValue(),adminViewDownLogPo.getStartPage(),adminViewDownLogPo.getPageSize());
        }
        adminViewDownLogPo.setSql(sql);
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.getLogList";
        List<AdminViewDownLogPo> cutList = sqlSession.selectList(statementId, adminViewDownLogPo,false);
        return cutList;
    }

    /**
     * 浏览下载统计,分页器显示的结果总数
     * @param adminViewDownLogPo
     * @return 分页器显示的结果总数
     */
    @Override
    public long cutCount(AdminViewDownLogPo adminViewDownLogPo) {
        ArrayList conditionList = new ArrayList();
        String sql = "";
        // 1 不分组
        if ("".equals(adminViewDownLogPo.getGroupType())){
            if (!"".equals(adminViewDownLogPo.getGroupValue())){
                conditionList.add(String.format("用户ID=\"*%s\"" , adminViewDownLogPo.getGroupValue()));
            }
            if (!"".equals(adminViewDownLogPo.getAccessType())){
                conditionList.add("访问类型=" + adminViewDownLogPo.getAccessType());
            }
            if (!"".equals(adminViewDownLogPo.getSource())){
                conditionList.add("资源来源=" + adminViewDownLogPo.getSource());
            }
            if (!"".equals(adminViewDownLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "\"" + adminViewDownLogPo.getStartTime()+ "\"");
            }
            if (!"".equals(adminViewDownLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "\"" + adminViewDownLogPo.getEndTime() + "\"");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select *  from YQTLLXZB %s",whereStr);
        }

        // 2 按 工号 分组
        else if ("worknum".equals(adminViewDownLogPo.getGroupType())){
            if (!"".equals(adminViewDownLogPo.getGroupValue())){
                conditionList.add(String.format("用户ID=\"*%s\"" , adminViewDownLogPo.getGroupValue()));
            }
            if (!"".equals(adminViewDownLogPo.getAccessType())){
                conditionList.add("访问类型=" + adminViewDownLogPo.getAccessType());
            }
            if (!"".equals(adminViewDownLogPo.getSource())){
                conditionList.add("资源来源=" + adminViewDownLogPo.getSource());
            }
            if (!"".equals(adminViewDownLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "\"" + adminViewDownLogPo.getStartTime()+ "\"");
            }
            if (!"".equals(adminViewDownLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "\"" + adminViewDownLogPo.getEndTime() + "\"");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select 用户ID,count(*) as count from YQTLLXZB %s group by 用户ID",whereStr);
        }else{

            // 3 按资源分组
            if (!"".equals(adminViewDownLogPo.getGroupValue())){
                conditionList.add(String.format("用户ID=\"*%s\"" , adminViewDownLogPo.getGroupValue()));
            }
            if (!"".equals(adminViewDownLogPo.getAccessType())){
                conditionList.add("访问类型=" + adminViewDownLogPo.getAccessType());
            }
            if (!"".equals(adminViewDownLogPo.getSource())){
                conditionList.add("资源来源=" + adminViewDownLogPo.getSource());
            }
            if (!"".equals(adminViewDownLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "\"" + adminViewDownLogPo.getStartTime()+ "\"");
            }
            if (!"".equals(adminViewDownLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "\"" + adminViewDownLogPo.getEndTime() + "\"");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select 资源名称,count(*)  from YQTLLXZB %s group by 资源名称",whereStr);
        }
        adminViewDownLogPo.setSql(sql);
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.getLogList";
        List<AdminViewDownLogPo> cutList = sqlSession.selectList(statementId, adminViewDownLogPo,false);
        return cutList.size();
    }

    /**
     * 浏览下载日志 导出
     * @param adminViewDownLogPo
     * @return
     */
    @Override
    public List<AdminViewDownLogPo> getResNotPaged(AdminViewDownLogPo adminViewDownLogPo) {
        ArrayList conditionList = new ArrayList();
        String sql = "";
        // 1 不分组
        if ("".equals(adminViewDownLogPo.getGroupType())){
            if (!"".equals(adminViewDownLogPo.getGroupValue())){
                conditionList.add(String.format("用户ID=\"*%s\"" , adminViewDownLogPo.getGroupValue()));
            }
            if (!"".equals(adminViewDownLogPo.getAccessType())){
                conditionList.add("访问类型=" + adminViewDownLogPo.getAccessType());
            }
            if (!"".equals(adminViewDownLogPo.getSource())){
                conditionList.add("资源来源=" + adminViewDownLogPo.getSource());
            }
            if (!"".equals(adminViewDownLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "\"" + adminViewDownLogPo.getStartTime()+ "\"");
            }
            if (!"".equals(adminViewDownLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "\"" + adminViewDownLogPo.getEndTime() + "\"");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select * from YQTLLXZB %s order by %s %s",whereStr,adminViewDownLogPo.getOrderType(),adminViewDownLogPo.getOrderValue());
        }

        // 2 按 用户id分组
        else if ("worknum".equals(adminViewDownLogPo.getGroupType())){
            if (!"".equals(adminViewDownLogPo.getGroupValue())){
                conditionList.add(String.format("用户ID=\"*%s\"" , adminViewDownLogPo.getGroupValue()));
            }
            if (!"".equals(adminViewDownLogPo.getAccessType())){
                conditionList.add("访问类型=" + adminViewDownLogPo.getAccessType());
            }
            if (!"".equals(adminViewDownLogPo.getSource())){
                conditionList.add("资源来源=" + adminViewDownLogPo.getSource());
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select 用户ID,count(*) as count from YQTLLXZB %s group by 用户ID order by %s %s",whereStr,adminViewDownLogPo.getOrderType(),adminViewDownLogPo.getOrderValue());


        }else{
            // 3 按资源名分组
//            if (!"".equals(adminViewDownLogPo.getGroupValue())){
//                conditionList.add(String.format("资源名称=\"*%s\"",adminViewDownLogPo.getGroupValue()));
//            }
            if (!"".equals(adminViewDownLogPo.getGroupValue())){
                conditionList.add(String.format("用户ID=\"*%s\"" , adminViewDownLogPo.getGroupValue()));
            }
            if (!"".equals(adminViewDownLogPo.getAccessType())){
                conditionList.add("访问类型=" + adminViewDownLogPo.getAccessType());
            }
            if (!"".equals(adminViewDownLogPo.getSource())){
                conditionList.add("资源来源=" + adminViewDownLogPo.getSource());
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select 资源名称,count(*) as count from YQTLLXZB %s group by 资源名称 order by %s %s",whereStr,adminViewDownLogPo.getOrderType(),adminViewDownLogPo.getOrderValue());
        }
        adminViewDownLogPo.setSql(sql);
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.getLogList";
        List<AdminViewDownLogPo> cutList = sqlSession.selectList(statementId, adminViewDownLogPo,false);
        return cutList;
    }

    @Override
    public List<VisitPo> getBrowseByOverallID(String getBrowseID) {
        if(!"".equals(getBrowseID) && getBrowseID!= null ){
            String[] browseID = getBrowseID.split(",");
            getBrowseID = BelongToSqlUtils.getBelongToSql(browseID, "全局唯一标识");
        }
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.getBrowseByOverallID";
        return sqlSession.selectList(statementId, getBrowseID, false);
    }

    /**
     * 用户登录日志 结果页10条数据
     * @param adminLoginLogPo
     * @return
     */
    @Override
    public List<AdminLoginLogPo> cutList_login(AdminLoginLogPo adminLoginLogPo){
        ArrayList conditionList = new ArrayList();
        String sql = "";
        // 1 不分组
        if ("".equals(adminLoginLogPo.getGroupType())){
            if (!"".equals(adminLoginLogPo.getGroupValue())){
                conditionList.add("用户ID=" + "'*" + adminLoginLogPo.getGroupValue() + "'");
            }
            if (!"".equals(adminLoginLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "'" + adminLoginLogPo.getStartTime()+ "'");
            }
            if (!"".equals(adminLoginLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "'" + adminLoginLogPo.getEndTime() + "'");
            }

            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select * from YQTDLB %s order by %s %s limit %s,%s",whereStr,adminLoginLogPo.getOrderType(),adminLoginLogPo.getOrderValue(),adminLoginLogPo.getStartPage(),adminLoginLogPo.getPageSize());
        }

        // 2 按 用户id分组
        else if ("worknum".equals(adminLoginLogPo.getGroupType())){
            if (!"".equals(adminLoginLogPo.getGroupValue())){
                conditionList.add("用户ID=" + "'*" + adminLoginLogPo.getGroupValue()  + "'");
            }
            if (!"".equals(adminLoginLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "'" + adminLoginLogPo.getStartTime()+ "'");
            }
            if (!"".equals(adminLoginLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "'" + adminLoginLogPo.getEndTime() + "'");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select 用户ID,count(*) as count from YQTDLB %s group by 用户ID order by %s %s limit %s,%s",whereStr,adminLoginLogPo.getOrderType(),adminLoginLogPo.getOrderValue(),adminLoginLogPo.getStartPage(),adminLoginLogPo.getPageSize());

        }
        else{
            // 3 按IP分组
//            if (!"".equals(adminLoginLogPo.getGroupValue())){
//                conditionList.add("IP地址=" + "'*" + adminLoginLogPo.getGroupValue()  + "'");
//            }
            if (!"".equals(adminLoginLogPo.getGroupValue())){
                conditionList.add("用户ID=" + "'*" + adminLoginLogPo.getGroupValue() + "'");
            }
            if (!"".equals(adminLoginLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "'" + adminLoginLogPo.getStartTime()+ "'");
            }
            if (!"".equals(adminLoginLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "'" + adminLoginLogPo.getEndTime() + "'");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select IP地址,count(*) as count from YQTDLB %s group by IP地址 order by %s %s limit %s,%s",whereStr,adminLoginLogPo.getOrderType(),adminLoginLogPo.getOrderValue(),adminLoginLogPo.getStartPage(),adminLoginLogPo.getPageSize());
        }
//        adminLoginLogPo.setSql(sql);
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.getLoginLogList";
        List<AdminLoginLogPo> cutList = sqlSession.selectList(statementId, sql,false);
        return cutList;

    };

    /**
     * 用户登录日志,分页器显示的结果总数
     * @param adminLoginLogPo
     * @return
     */
    @Override
    public long cutCount_login(AdminLoginLogPo adminLoginLogPo) {
        ArrayList conditionList = new ArrayList();
        String sql = "";
        // 1 不分组
        if ("".equals(adminLoginLogPo.getGroupType())){
            if (!"".equals(adminLoginLogPo.getGroupValue())){
                conditionList.add("用户ID=" + "'*" + adminLoginLogPo.getGroupValue() + "'");
            }
            if (!"".equals(adminLoginLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "'" +adminLoginLogPo.getStartTime() + "'");
            }
            if (!"".equals(adminLoginLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "'" + adminLoginLogPo.getEndTime() + "'");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select * from YQTDLB %s",whereStr);
        }

        // 2 按 工号 分组
        else if ("worknum".equals(adminLoginLogPo.getGroupType())){
            if (!"".equals(adminLoginLogPo.getGroupValue())){
                conditionList.add("用户ID=" + "'*" + adminLoginLogPo.getGroupValue()  + "'");
            }
            if (!"".equals(adminLoginLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "'" + adminLoginLogPo.getStartTime()+ "'");
            }
            if (!"".equals(adminLoginLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "'" + adminLoginLogPo.getEndTime() + "'");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select 用户ID,count(*) as count from YQTDLB %s group by 用户ID",whereStr);

        }
        else{

            // 3 按IP分组
//            if (!"".equals(adminLoginLogPo.getGroupValue())){
//                conditionList.add("IP地址=" + "'*" + adminLoginLogPo.getGroupValue()  + "'");
//            }
            if (!"".equals(adminLoginLogPo.getGroupValue())){
                conditionList.add("用户ID=" + "'*" + adminLoginLogPo.getGroupValue() + "'");
            }
            if (!"".equals(adminLoginLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "'" + adminLoginLogPo.getStartTime()+ "'");
            }
            if (!"".equals(adminLoginLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "'" + adminLoginLogPo.getEndTime() + "'");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select IP地址,count(*)  from YQTDLB %s group by IP地址",whereStr);
        }
//        adminLoginLogPo.setSql(sql);
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.getLoginLogCount";
        List<AdminLoginLogPo> cutList = sqlSession.selectList(statementId, sql,false);
        return cutList.size();
    }


    /**
     * 登录日志 导出结果
     * @param adminLoginLogPo
     * @return
     */
    @Override
    public List<AdminLoginLogPo> getResNotPaged_login(AdminLoginLogPo adminLoginLogPo) {
        ArrayList conditionList = new ArrayList();
        String sql = "";
        // 1 不分组
        if ("".equals(adminLoginLogPo.getGroupType())){
            if (!"".equals(adminLoginLogPo.getGroupValue())){
                conditionList.add("用户ID=" + "'*" + adminLoginLogPo.getGroupValue() + "'");
            }
            if (!"".equals(adminLoginLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "'" + adminLoginLogPo.getStartTime()+ "'");
            }
            if (!"".equals(adminLoginLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "'" + adminLoginLogPo.getEndTime() + "'");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select * from YQTDLB %s order by %s %s",whereStr,adminLoginLogPo.getOrderType(),adminLoginLogPo.getOrderValue());
        }

        // 2 按 用户id分组
        else if ("worknum".equals(adminLoginLogPo.getGroupType())){
            if (!"".equals(adminLoginLogPo.getGroupValue())){
                conditionList.add("用户ID=" + "'*" + adminLoginLogPo.getGroupValue()  + "'");
            }
            if (!"".equals(adminLoginLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "'" + adminLoginLogPo.getStartTime()+ "'");
            }
            if (!"".equals(adminLoginLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "'" + adminLoginLogPo.getEndTime() + "'");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select 用户ID,count(*) as count from YQTDLB %s group by 用户ID order by %s %s",whereStr,adminLoginLogPo.getOrderType(),adminLoginLogPo.getOrderValue());

        }
        else{
            // 3 按IP分组
//            if (!"".equals(adminLoginLogPo.getGroupValue())){
//                conditionList.add("IP地址=" + "'*" + adminLoginLogPo.getGroupValue()  + "'");
//            }
            if (!"".equals(adminLoginLogPo.getGroupValue())){
                conditionList.add("用户ID=" + "'*" + adminLoginLogPo.getGroupValue() + "'");
            }
            if (!"".equals(adminLoginLogPo.getStartTime())){
                conditionList.add("创建时间>=" + "'" + adminLoginLogPo.getStartTime()+ "'");
            }
            if (!"".equals(adminLoginLogPo.getEndTime())){
                conditionList.add("创建时间<=" + "'" + adminLoginLogPo.getEndTime() + "'");
            }
            String whereStr = "";
            if (conditionList.size()!=0){
                whereStr = String.format("where %s",String.join(" and ",conditionList));
            }
            sql = String.format("select IP地址,count(*) as count from YQTDLB %s group by IP地址 order by %s %s",whereStr,adminLoginLogPo.getOrderType(),adminLoginLogPo.getOrderValue());
        }

        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.AdminLogDao.getLoginLogList";
        List<AdminLoginLogPo> cutList = sqlSession.selectList(statementId, sql,false);
        return cutList;
    }

    @Override
    public List<VisitPo> getLongBrowseByOverallID(String getBrowseID) {
        List<VisitPo> result = null;
        String sql = "SELECT 全局唯一标识 as overallid, 浏览量 as browse FROM  YQTLLLTJ";
        if(!"".equals(getBrowseID) && getBrowseID!= null ){
            String[] browseID = getBrowseID.split(",");
            getBrowseID = " WHERE "+ BelongToSqlUtils.getBelongToSql(browseID, "全局唯一标识");
        }
        sql += getBrowseID;
        result = dataContext.excuteQuery(sql,VisitPo.class);
        return result;
    }

    @Override
    public List<MonitorPo> cutPage_Monitor(MonitorBo bo){
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.MonitorDao.cutPage_Monitor";
        List<MonitorPo> cutList =  sqlSession.selectList(statementId,bo,false);
        return cutList;
    };


    @Override
    public long cutCount_Monitor (MonitorBo bo){
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.MonitorDao.cutCount_Monitor";
        long num=sqlSession.getCount(statementId,bo);
        return num;
    };

    @Override
    public List<MonitorPo> exportMonitorLog(MonitorBo bo){
        SqlSession sqlSession = DataSourceUtil.getSqlSession();
        String statementId = "jzqb.dao.MonitorDao.exportMonitorLog";
        List<MonitorPo> res =  sqlSession.selectList(statementId,bo,false);
        return res;
    };
}
